#include "builtin.h"
#include "externs.h"
#include "parse.h"
#include <stdlib.h>
#include <stdio.h>

typedef void (*CMD_HANDLER) (void);
typedef struct builtin_cmd
{
    char *cmd;
    CMD_HANDLER handler;
} BUILTIN_CMD;

void do_exit (void);
void do_cd (void);
void do_type (void);

BUILTIN_CMD builtins[] = {
    {"exit", do_exit},
    {"cd", do_cd},
    {"type", do_type},
    {NULL, NULL}
};

/**
 * 内部命令解析
 * 返回1表示内部命令,0表示非内部命令
 */
int builtin(void)
{
    int i = 0;
    int executed = FALSE;
    while (builtins[i].cmd) {
        if (check (builtins[i].cmd)) {
            builtins[i].handler ();
            executed = TRUE;
            break;
        }
    }
    return executed;
}

void do_exit (void) {
    printf ("Exit\n");
    exit (EXIT_SUCCESS);
}

void do_cd (void) {
    printf ("do_cd ...\n");
}

void do_type (void) {
    printf ("do_type ...\n");
}
